package com.cyq.dascma.controller;

import cn.hutool.core.util.IdUtil;
import com.cyq.dascma.constant.ValidaEnum;
import com.cyq.dascma.dto.ScheduleDto;
import com.cyq.dascma.dto.ScoreInfoDto;
import com.cyq.dascma.entity.Course;
import com.cyq.dascma.entity.Schedule;
import com.cyq.dascma.entity.Student;
import com.cyq.dascma.exception.ServiceException;
import com.cyq.dascma.handler.valida.impl.CourseValidaImpl;
import com.cyq.dascma.handler.valida.impl.ScheduleValidaImpl;
import com.cyq.dascma.result.Result;
import com.cyq.dascma.result.ResultGenerator;
import com.cyq.dascma.service.ScheduleService;
import com.cyq.dascma.util.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * dingwen
 * 2021/4/4 18:00
 **/
@Api(tags = "课程表管理")
@RestController
@RequestMapping("/schedule")
public class ScheduleController {
    private final ScheduleService scheduleService;

    @Autowired
    ScheduleController(ScheduleService scheduleService) {
        this.scheduleService = scheduleService;
    }

    /*
     *新增课程表日程信息
     * @param [schedule]
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("新增课程表日程信息")
    @ApiImplicitParam(value = "课程表", name = "schedule", required = true, dataType = "schedule", paramType = "body")
    @PostMapping("/add")
    public Result add(@RequestBody Schedule schedule) {
        schedule.setScheduleId(IdUtil.randomUUID());
        try {
            new ScheduleValidaImpl().valida(ValidaEnum.ADD, schedule);
        } catch (ServiceException serviceException) {
            return serviceException.result();
        }

        boolean flag = scheduleService.save(schedule);

        return flag ? ResultGenerator.genOkResult(schedule).setMessage("成功新增程表日程信息") : ResultGenerator.genFailureResult("新增课程表日程信息失败");
    }

    /*
     *更新课程表信息
     * @param [schedule]
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("更新课程表信息")
    @ApiImplicitParam(value = "课程表", name = "schedule", required = true, dataType = "schedule", paramType = "body")
    @PutMapping("/modify")
    public Result modify(@RequestBody Schedule schedule) {
        try {
            new ScheduleValidaImpl().valida(ValidaEnum.MODIFY, schedule);
        } catch (ServiceException serviceException) {
            return serviceException.result();
        }

        boolean flag = scheduleService.updateById(schedule);

        return flag ? ResultGenerator.genOkResult(schedule).setMessage("更新课程表信息成功") : ResultGenerator.genFailureResult("更新课程表信息失败");
    }

    /*
     *删除课程表信息
     * @param [scheduleId]
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("删除课程表信息")
    @ApiImplicitParam(value = "课程表ID", name = "scheduleId")
    @DeleteMapping("/remove/{scheduleId}")
    public Result remove(@PathVariable String scheduleId) {
        boolean flag = scheduleService.removeById(scheduleId);
        return flag ? ResultGenerator.genOkResult(scheduleId).setMessage("删除课程表信息成功") : ResultGenerator.genFailureResult("删除课程表信息失败");
    }


    /*
     *查询课程表信息
     * @param []
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation(value = "查询课程表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scheduleYear", value = "学年", required = false),
            @ApiImplicitParam(name = "scheduleType", value = "学期【0：上学期 1：下学期】", required = false),
            @ApiImplicitParam(name = "clazzId", value = "班级编号", required = false),
            @ApiImplicitParam(name = "teacherId", value = "授课教师教工号", required = false),
            @ApiImplicitParam(name = "teacherName", value = "授课教师姓名", required = false),
    })

    @GetMapping("info/page")
    public Result get(@RequestParam(value = "scheduleYear", required = false) String scheduleYear,
                      @RequestParam(value = "scheduleType", required = false) String scheduleType,
                      @RequestParam(value = "clazzId", required = false) String clazzId,
                      @RequestParam(value = "teacherId", required = false) String teacherId,
                      @RequestParam(value = "teacherName", required = false) String teacherName,
                      HttpServletRequest request) {
        Map<String, Object> params = RequestUtil.getParameterMap(request);

        List<ScheduleDto> scoreInfoDtoList = scheduleService.findScheduleInfoList(params);
        return ResultGenerator.genOkResult(scoreInfoDtoList);
    }

    /*
     *查询课程表表总记录条数
     * @param []
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("查询课程表表总记录条数")
    @GetMapping("/total")
    public Result getTotal() {
        return ResultGenerator.genOkResult(scheduleService.count());
    }

    /*
     *批量删除
     * @param [scheduleList]
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("批量删除")
    @ApiImplicitParam(name = "scheduleList", value = "课程表List", paramType = "body")
    @DeleteMapping("/remove/batch")
    public Result remove(@RequestBody List<Schedule> scheduleList) {
        List<String> ids = new ArrayList<>();
        scheduleList.stream().parallel().forEach(schedule -> ids.add(schedule.getScheduleId()));
        boolean flag = scheduleService.removeByIds(ids);
        return flag ? ResultGenerator.genOkResult() : ResultGenerator.genFailureResult("批量删除失败");
    }

}
